package com.engine.cn.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 队列交换机持久化
 */
@Configuration
public class RabbitQueueConfig {
    // 持久化交换机
    @Bean
    public DirectExchange durableExchange() {
        return new DirectExchange("demo.exchange", true, false);
    }

    //持久化队列，durable就是持久化的意思
    @Bean
    public Queue durableQueue() {
        Map<String, Object> args = new HashMap<>();
        // 设置死信交换机
        args.put("x-dead-letter-exchange", "dlx.exchange");
        // 设置死信路由键
        args.put("x-dead-letter-routing-key", "dlx.routing.key");
        return new Queue("durable.queue", true, false, false, args);
    }

    // 死信交换机
    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange("dlx.exchange", true, false);
    }

    // 死信队列
    @Bean
    public Queue dlxQueue() {
        return new Queue("dlx.queue", true);
    }

    // 死信绑定
    @Bean
    public Binding dlxBinding() {
        return BindingBuilder.bind(dlxQueue())
                .to(dlxExchange())
                .with("dlx.routing.key");
    }
}
